{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray的特性",
   "id": "3079945050d9756f"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 多维性\n",
   "id": "e626c9f86226817"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T10:24:59.738062Z",
     "start_time": "2025-07-27T10:24:59.572887Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "print(np.__version__)"
   ],
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.2\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr的维度为：0\n"
     ]
    }
   ],
   "execution_count": 3,
   "source": [
    "# 创建一个0维的ndarray的数组\n",
    "arr = np.array(1)\n",
    "print(f'arr的维度为：{arr.ndim}')"
   ],
   "id": "2590fcf00793888e"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr的维度为：1\n",
      "arr: [0 1 2]\n"
     ]
    }
   ],
   "execution_count": 5,
   "source": [
    "# 创建一个1维的ndarray数组，即向量\n",
    "arr = np.array([0, 1, 2])\n",
    "print(f'arr的维度为：{arr.ndim}')\n",
    "print(f'arr: {arr}')"
   ],
   "id": "e6e0b5ae8f47eae3"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr: [[0 1 2]\n",
      " [3 4 5]], 维度为 2\n"
     ]
    }
   ],
   "execution_count": 7,
   "source": [
    "# 创建一个2维的ndarray数组，即矩阵\n",
    "arr = np.array([[0, 1, 2], [3, 4, 5]])\n",
    "print(f'arr: {arr}, 维度为 {arr.ndim}')"
   ],
   "id": "d523871d1fdd2b7c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 同质性\n",
   "id": "39c4af7d5703154"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-26T12:43:28.919989Z",
     "start_time": "2025-07-26T12:43:28.902551Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, '1']) # 当ndarray中存在不同类型的元素时，np会默认将其转换为相同类型的元素\n",
    "print(arr)"
   ],
   "id": "5205db1d3078fb96",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1' '1']\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-26T12:47:41.130577Z",
     "start_time": "2025-07-26T12:47:41.119531Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2.5]) # 数据元素：number float 转换为 float float\n",
    "print(arr)"
   ],
   "id": "42804de0f6bc7696",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.  2.5]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 2.ndarray的常用属性",
   "id": "5629213b8175e913"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-26T13:11:21.441607Z",
     "start_time": "2025-07-26T13:11:21.419667Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2, 3])\n",
    "arr2 = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "arr3 = np.array([[[1, 2, 3], [4, 5, 6]]])\n",
    "print(f'arr3: {arr3}')\n",
    "print(f'arr3的转置：{arr3.T}')\n",
    "arr4 = np.array([[[[1, 2, 3], [4, 5, 6]]], [[[1, 2, 3], [4, 5, 6]]]])\n",
    "print(f'arr的形状：{arr.shape}, 维度: {arr.ndim}, 元素总数: {arr.size}, 元素类型：{arr.dtype}')\n",
    "print(f'arr2的形状: {arr2.shape} 维度: {arr2.ndim}, 元素总数: {arr2.size}, 元素类型：{arr2.dtype}')\n",
    "print(f'arr3的形状：{arr3.shape} 维度: {arr3.ndim}, 元素总数: {arr3.size}, 元素类型：{arr3.dtype}')\n",
    "print(f'arr4的形状：{arr4.shape} 维度: {arr4.ndim}, 元素总数: {arr4.size}, 元素类型：{arr4.dtype}')"
   ],
   "id": "9691a5a425e2cca2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr3: [[[1 2 3]\n",
      "  [4 5 6]]]\n",
      "arr3的转置：[[[1]\n",
      "  [4]]\n",
      "\n",
      " [[2]\n",
      "  [5]]\n",
      "\n",
      " [[3]\n",
      "  [6]]]\n",
      "arr的形状：(3,), 维度: 1, 元素总数: 3, 元素类型：int64\n",
      "arr2的形状: (2, 3) 维度: 2, 元素总数: 6, 元素类型：int64\n",
      "arr3的形状：(1, 2, 3) 维度: 3, 元素总数: 6, 元素类型：int64\n",
      "arr4的形状：(2, 1, 2, 3) 维度: 4, 元素总数: 12, 元素类型：int64\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T10:22:17.587397Z",
     "start_time": "2025-07-27T10:22:17.151023Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])  # 形状 (2, 2, 2)\n",
    "# 分析三维ndarray对象的取法\n",
    "print(arr_3d[:, 0])\n",
    "print('-' * 50)\n",
    "print(arr_3d[:, :, 0])\n"
   ],
   "id": "ad38121b8e015909",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[1], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m arr_3d \u001B[38;5;241m=\u001B[39m \u001B[43mnp\u001B[49m\u001B[38;5;241m.\u001B[39marray([[[\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m], [\u001B[38;5;241m3\u001B[39m, \u001B[38;5;241m4\u001B[39m]], [[\u001B[38;5;241m5\u001B[39m, \u001B[38;5;241m6\u001B[39m], [\u001B[38;5;241m7\u001B[39m, \u001B[38;5;241m8\u001B[39m]]])  \u001B[38;5;66;03m# 形状 (2, 2, 2)\u001B[39;00m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28mprint\u001B[39m(arr_3d[:, \u001B[38;5;241m0\u001B[39m])\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m-\u001B[39m\u001B[38;5;124m'\u001B[39m \u001B[38;5;241m*\u001B[39m \u001B[38;5;241m50\u001B[39m)\n",
      "\u001B[1;31mNameError\u001B[0m: name 'np' is not defined"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-26T13:53:41.796253Z",
     "start_time": "2025-07-26T13:53:41.776306Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "arr2_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) # shape: (2, 2, 3)\n",
    "print(f'arr2_3d shape: {arr2_3d.shape}')\n",
    "print(arr2_3d[:, 0]) # [ [1, 2, 3], [7, 8, 9] ]\n",
    "print('-' * 50)\n",
    "print(arr2_3d[:, :, 0]) # [ [1, 4], [7, 10] ]"
   ],
   "id": "58ee6d1e93769615",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr2_3d shape: (2, 2, 3)\n",
      "[[1 2 3]\n",
      " [7 8 9]]\n",
      "--------------------------------------------------\n",
      "[[ 1  4]\n",
      " [ 7 10]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-26T14:07:49.380027Z",
     "start_time": "2025-07-26T14:07:49.367062Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.arange(24).reshape(2, 3, 4)  # 形状 (2, 3, 4)\n",
    "print(\"Original array:\\n\", arr)"
   ],
   "id": "4d47773e4a537e54",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original array:\n",
      " [[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-26T14:08:13.536615Z",
     "start_time": "2025-07-26T14:08:13.519703Z"
    }
   },
   "cell_type": "code",
   "source": [
    "transposed = arr.T  # 等价于 transpose(2, 1, 0)\n",
    "print(\"Transposed shape:\", transposed.shape)  # (4, 3, 2)\n",
    "print(\"Transposed array:\\n\", transposed     )"
   ],
   "id": "9e251f5dc593c0a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transposed shape: (4, 3, 2)\n",
      "Transposed array:\n",
      " [[[ 0 12]\n",
      "  [ 4 16]\n",
      "  [ 8 20]]\n",
      "\n",
      " [[ 1 13]\n",
      "  [ 5 17]\n",
      "  [ 9 21]]\n",
      "\n",
      " [[ 2 14]\n",
      "  [ 6 18]\n",
      "  [10 22]]\n",
      "\n",
      " [[ 3 15]\n",
      "  [ 7 19]\n",
      "  [11 23]]]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T10:27:01.713999Z",
     "start_time": "2025-07-27T10:27:01.700038Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.arange(24) # 获取一个1维的ndarray对象，且其中的元素一次为0~23\n",
    "print(f'arr: {arr}')\n",
    "print(f'itemsize: {arr.itemsize}') # 元素的大小\n",
    "print(f'nbytes: {arr.nbytes}') # 全部元素所占的大小\n",
    "print(f'flags: {arr.flags}')"
   ],
   "id": "98563bee90c87732",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "itemsize: 8\n",
      "nbytes: 192\n",
      "flags:   C_CONTIGUOUS : True\n",
      "  F_CONTIGUOUS : True\n",
      "  OWNDATA : True\n",
      "  WRITEABLE : True\n",
      "  ALIGNED : True\n",
      "  WRITEBACKIFCOPY : False\n",
      "\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 3. ndarray的创建1\n",
   "id": "c2ceac49b50b450f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T10:41:29.301755Z",
     "start_time": "2025-07-27T10:41:29.285798Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "arr2 = arr\n",
    "arr2[0] = -1\n",
    "print(f'{arr}')\n",
    "print(f'{arr2}')"
   ],
   "id": "d8a6155e05de466f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1 -1 -1]\n",
      " [ 4  5  6]]\n",
      "[[-1 -1 -1]\n",
      " [ 4  5  6]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:03:54.504109Z",
     "start_time": "2025-07-27T12:03:54.495133Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 预定义形状\n",
    "arr = np.zeros(shape=(3, 3), dtype=int) # 创建一个2维的 3*3 ndarray对象，数据元素全为0，数据类型为int\n",
    "print(f'arr: {arr}')"
   ],
   "id": "a5742e903bb7dbd7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr: [[0 0 0]\n",
      " [0 0 0]\n",
      " [0 0 0]]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:06:14.867834Z",
     "start_time": "2025-07-27T12:06:14.858387Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 全1\n",
    "arr = np.ones(shape=(2, 3, 4), dtype=int)\n",
    "print(f'arr = {arr}')"
   ],
   "id": "14a031f61873f3b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr = [[[1 1 1 1]\n",
      "  [1 1 1 1]\n",
      "  [1 1 1 1]]\n",
      "\n",
      " [[1 1 1 1]\n",
      "  [1 1 1 1]\n",
      "  [1 1 1 1]]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:11:01.110827Z",
     "start_time": "2025-07-27T12:11:01.093839Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 未初始化的数据元素\n",
    "arr111 = np.empty((2, 3), dtype=int)\n",
    "print(arr111)"
   ],
   "id": "2835ab1bf0193cf6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [0 0 0]]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:11:55.432850Z",
     "start_time": "2025-07-27T12:11:55.421879Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 未知元素随机初始化\n",
    "arr = np.empty((2, 3, 4))\n",
    "print(arr)"
   ],
   "id": "cc66b82b361b3364",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[5.e-324 5.e-324 5.e-324 5.e-324]\n",
      "  [5.e-324 5.e-324 5.e-324 5.e-324]\n",
      "  [5.e-324 5.e-324 5.e-324 5.e-324]]\n",
      "\n",
      " [[5.e-324 5.e-324 5.e-324 5.e-324]\n",
      "  [5.e-324 5.e-324 5.e-324 5.e-324]\n",
      "  [5.e-324 5.e-324 5.e-324 5.e-324]]]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:18:28.880686Z",
     "start_time": "2025-07-27T12:18:28.862739Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 指定填充的数字\n",
    "arr = np.full((2, 2), fill_value=10, dtype=float) # 用10填充满ndarray对象\n",
    "print(arr)"
   ],
   "id": "7fb83f70a0b619ad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10. 10.]\n",
      " [10. 10.]]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:23:12.318844Z",
     "start_time": "2025-07-27T12:23:12.301928Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 基于一个ndarray对象创建一个形状相同的对象\n",
    "arr_full_zero = np.zeros_like(arr)\n",
    "print(f'arr_full_zero: {arr_full_zero}')\n",
    "\n",
    "arr_empty = np.empty_like(arr)\n",
    "print(f'arr_empty: {arr_empty}')\n",
    "\n",
    "arr_full_one = np.ones_like(arr)\n",
    "print(f'arr_full_one: {arr_full_one}')\n",
    "\n",
    "arr_full = np.full_like(arr, fill_value=2025)\n",
    "print(f'arr_full: {arr_full}')"
   ],
   "id": "5fef064d2527aa94",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr_full_zero: [[0. 0.]\n",
      " [0. 0.]]\n",
      "arr_empty: [[0. 0.]\n",
      " [0. 0.]]\n",
      "arr_full_one: [[1. 1.]\n",
      " [1. 1.]]\n",
      "arr_full: [[2025. 2025.]\n",
      " [2025. 2025.]]\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:38:16.003428Z",
     "start_time": "2025-07-27T12:38:15.983834Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 等差数列\n",
    "arr = np.arange(1, 10, 1) # start end step，值得注意的是，end是取不到的\n",
    "print(arr)"
   ],
   "id": "9e5695c10edf3ffd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T12:49:20.671549Z",
     "start_time": "2025-07-27T12:49:20.655079Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 等间隔舒蕾\n",
    "arr = np.linspace(0, 10, 5) # start end num, 注意num是指start -> end 之前取等间隔的几个数（含首和尾）\n",
    "print(arr)\n",
    "\n",
    "arr2 = np.arange(0, 11, 2.5)\n",
    "print(arr2)"
   ],
   "id": "972ac6cbc9f82db2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.   2.5  5.   7.5 10. ]\n",
      "[ 0.   2.5  5.   7.5 10. ]\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T13:05:18.203275Z",
     "start_time": "2025-07-27T13:05:18.193246Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对数间隔数列\n",
    "arr = np.logspace(0, 4, 3,base=2)\n",
    "arr2 = 2 ** np.linspace(0, 4, 3)\n",
    "print(arr)\n",
    "print(arr2)\n",
    "# 1.024e+03: 1.024乘以10的3次方，即1024"
   ],
   "id": "31f2ce9c47a52571",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  4. 16.]\n",
      "[ 1.  4. 16.]\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T13:35:28.861507Z",
     "start_time": "2025-07-27T13:35:28.853528Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 特殊矩阵\n",
    "# 单位矩阵：主对角线的数字为1，其余全为0\n",
    "arr = np.eye(3, dtype=int)\n",
    "print(arr)\n",
    "\n",
    "arr = np.eye(3, 4, dtype=int)\n",
    "print(arr)"
   ],
   "id": "6974377032da02e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0]\n",
      " [0 1 0]\n",
      " [0 0 1]]\n",
      "[[1 0 0 0]\n",
      " [0 1 0 0]\n",
      " [0 0 1 0]]\n"
     ]
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T13:38:26.415962Z",
     "start_time": "2025-07-27T13:38:26.407983Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对角矩阵：主对角的元素非0，其余均为0\n",
    "arr = np.diag([1, 2, 3, 4])\n",
    "print(arr)"
   ],
   "id": "135f5eeaa52374f5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0]\n",
      " [0 2 0 0]\n",
      " [0 0 3 0]\n",
      " [0 0 0 4]]\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T13:51:43.652092Z",
     "start_time": "2025-07-27T13:51:43.632146Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机数组的生成\n",
    "# 生成0~1的随机浮点数 （均匀分布，即随机生成的每个元素的概率都是一样的）\n",
    "arr = np.random.rand(2, 3) # params: rows cols \n",
    "print(arr)"
   ],
   "id": "b3f3bed318a3fc5a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.29825619 0.07806525 0.65746952]\n",
      " [0.44404391 0.4695715  0.46332484]]\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T14:03:34.017408Z",
     "start_time": "2025-07-27T14:03:34.006418Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成指定区间的随机浮点数\n",
    "arr = np.random.uniform(3, 6, (2, 3)) # start end shape:tuple 注：end可以取到\n",
    "print(arr)"
   ],
   "id": "8d288cd6208135c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.52446021 5.18810211 3.28427767]\n",
      " [4.90869282 5.88512054 5.75570202]]\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T14:05:46.152830Z",
     "start_time": "2025-07-27T14:05:46.129891Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成指定范围的int整数\n",
    "arr = np.random.randint(3, 6, (2, 3)) # start end shape:tuple 注：end可以取到\n",
    "print(arr)"
   ],
   "id": "b96a560b62f6b99d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 4 3]\n",
      " [5 5 5]]\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T14:12:01.207029Z",
     "start_time": "2025-07-27T14:12:01.190074Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成随机数列 （正态分布）\n",
    "# 正态分布：2边小 中间大\n",
    "arr = np.random.randn(2, 3)\n",
    "print(arr)"
   ],
   "id": "d8e0b42704f13c7f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.32783893  0.39712949 -1.20906214]\n",
      " [-1.06636653 -0.23980427 -1.39436379]]\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-27T14:25:19.150462Z",
     "start_time": "2025-07-27T14:25:19.133417Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置随机种子\n",
    "\"\"\"\n",
    "# 3是一个指定的“随机种子编号”，只要保持一样，随机数序列就一样；它本身可以是任意整数，目的是为了让结果“可控# 、可复现”，不是必需，但在科学实验或调试中非常有用\n",
    "\"\"\"\n",
    "np.random.seed(3) \n",
    "np.random.seed(20)\n",
    "arr = np.random.randint(1, 10, (1, 10))\n",
    "print(arr)"
   ],
   "id": "f36926adddddcc9a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 5 7 8 3 1 7 9 6 4]]\n"
     ]
    }
   ],
   "execution_count": 111
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
